package com.hei.controller;

import com.hei.pojo.Buser;
import com.hei.service.BuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private BuserService buserService;

    @PostMapping("/login")
    public String login(Buser user, HttpSession session, Model model){
        boolean b = buserService.loginSuccess(user);
        if(b){
             user = buserService.selectUserByAccount(user.getAccount());
            System.out.println(user);
            session.setAttribute("user",user);
            return "redirect:/manage/manage";
        }
        model.addAttribute("msg","账号或密码错误");
        return "login";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/";
    }
}
